package data;

import java.util.ArrayList;
import java.util.Collections;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import models.GeoModel;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

public class GeoData {
	private Objectify ofy;

	private Cache cache = null;

	private String prefix = "geo_";

	private int limit = 20;

	private int page = 1;

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public GeoData() {
		try {
			ObjectifyService.register(GeoModel.class);
		} catch (Exception e) {
		}
		try {
			cache = CacheManager.getInstance().getCacheFactory()
					.createCache(Collections.emptyMap());
		} catch (CacheException e) {
		}
		ObjectifyOpts opts = new ObjectifyOpts().setSessionCache(true);
		ofy = ObjectifyService.begin(opts);
	}

	public ArrayList<GeoModel> list() {
		ArrayList<GeoModel> geos = new ArrayList<GeoModel>();

		int start = (page - 1) * limit;
		Query<GeoModel> q = ofy.query(GeoModel.class).limit(limit)
				.offset(start);
		for (GeoModel geo : q) {
			geos.add(geo);
		}

		return geos;
	}

	@SuppressWarnings("unchecked")
	public void insert(GeoModel geo) {
		if (cache != null) {
			cache.put(prefix + geo.getGeoId(), geo);
		}
		ofy.put(geo);
	}

	public void delete(GeoModel geo) {
		if (cache != null && cache.containsKey(prefix + geo.getGeoId())) {
			cache.remove(prefix + geo.getGeoId());
		}
		ofy.delete(geo);
	}

	@SuppressWarnings("unchecked")
	public GeoModel getGeo(String id) {
		GeoModel geo = new GeoModel();
		if (cache != null && cache.containsKey(prefix + id)) {
			geo = (GeoModel) cache.get(prefix + id);
		} else {
			try {
				geo = ofy.get(new Key<GeoModel>(GeoModel.class, id));
				cache.put(prefix + id, geo);
			} catch (Exception e) {
				geo = null;
			}
		}
		return geo;
	}

	public GeoModel getFirstCity() {
		GeoModel geo = new GeoModel();
		if (cache != null && cache.containsKey(prefix + "first_city")) {
			geo = (GeoModel) cache.get(prefix + "first_city");
		} else {
			geo = ofy.query(GeoModel.class).filter("parentId", "").get();
			cache.put(prefix + "first_city", geo);
		}
		
		
		return geo;
	}

	public ArrayList<GeoModel> getCities() {
		ArrayList<GeoModel> geos = new ArrayList<GeoModel>();

		if (cache != null && cache.containsKey(prefix + "cities")) {
			geos = (ArrayList<GeoModel>) cache.get(prefix + "cities");
		} else {
			Query<GeoModel> q = ofy.query(GeoModel.class).filter("parentId", "");
			for (GeoModel geo : q) {
				geos.add(geo);
			}
			cache.put(prefix + "cities", geos);
		}

		return geos;
	}

	@SuppressWarnings("unchecked")
	public GeoModel getGeoByAlias(String alias) {
		GeoModel geo = new GeoModel();
		if (cache != null && cache.containsKey(prefix + alias)) {
			geo = (GeoModel) cache.get(prefix + alias);
		} else {
			try {
				geo = ofy.query(GeoModel.class).filter("alias", alias).get();
				cache.put(prefix + alias, geo);
			} catch (Exception e) {
				geo = null;
			}
		}
		return geo;
	}

	public ArrayList<GeoModel> getChildrenById(String id) {
		ArrayList<GeoModel> geos = new ArrayList<GeoModel>();

		Query<GeoModel> q = ofy.query(GeoModel.class).filter("parentId", id);
		for (GeoModel geo : q) {
			geos.add(geo);
		}

		return geos;
	}

	public ArrayList<GeoModel> getAbc() {
		ArrayList<GeoModel> geos = new ArrayList<GeoModel>();

		Query<GeoModel> q = ofy.query(GeoModel.class)
				.filter("parents", "RFWFQ9VJ,RFXMX9GR,RF4S66DI")
				.filter("parentId", "RFXMYJKM").filter("alias", "quan-hoa")
				.filter("geoId", "RF4S8SE2");
		for (GeoModel geo : q) {
			geos.add(geo);
		}

		return geos;
	}
}
